package main

import (
	"image"
	"image/color"
	"image/png"
	"os"
)

const (
	width       = 1000000
	height      = 1000000
	chunkHeight = 1000 // Define the chunk size for processing
)

func main() {
	// Create a file to save the image
	file, err := os.Create("stupidly_large_black.png")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	// Create an empty image with the final dimensions
	img := image.NewRGBA(image.Rect(0, 0, width, height))

	// Create and stack chunks of black image
	for y := 0; y < height; y += chunkHeight {
		chunkEnd := y + chunkHeight
		if chunkEnd > height {
			chunkEnd = height
		}
		chunk := createChunk(width, chunkEnd-y)

		// Stack the chunk on the image
		stackImage(img, chunk, y)

		// Encode the image as PNG and write to the file
		err = png.Encode(file, img)
		if err != nil {
			panic(err)
		}
	}
}

// createChunk creates a black image chunk with the specified width and height
func createChunk(width, height int) *image.RGBA {
	img := image.NewRGBA(image.Rect(0, 0, width, height))
	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			img.Set(x, y, color.Black)
		}
	}
	return img
}

// stackImage stacks img2 on top of img1 starting from the specified y offset
func stackImage(img1, img2 *image.RGBA, offsetY int) {
	for y := 0; y < img2.Bounds().Dy(); y++ {
		for x := 0; x < img2.Bounds().Dx(); x++ {
			img1.Set(x, y+offsetY, img2.At(x, y))
		}
	}
}
